local quanyi_viewas = fk.CreateSkill{
  name = "#quanyi_viewas",
}

Fk:loadTranslationTable{
  ["xiaobai-twelve__zhaoqi"] = "赵岐",
  ["#xiaobai-twelve__zhaoqi"] = "苦无时命",
  ["designer:xiaobai-twelve__zhaoqi"] = "扬林",
  ["illustrator:xiaobai-twelve__zhaoqi"] = "强强&小猫",

  ["xiaobai__quanyi"] = "权宜",
  [":xiaobai__quanyi"] = "每轮限一次，你的上家回合结束时，你可以与下家交换位置，"..
    "然后可视为使用一张未以此法使用过的基本牌或普通锦囊牌，若你曾与该角色交换过位置，则你再视为使用任意张以此法使用过的牌，然后失去此技能。",
  ["#quanyi_viewAs"] = "权宜",
  ["#xiaobai__quanyi_prompt"] = "权宜：你可以与 %dest 交换位次",
  ["#xiaobai__quanyi_use_prompt"] = "权宜：你可以视为使用一张基本牌或普通锦囊牌",
  ["@$xiaobai__quanyi"] = "权宜",
  ["@@xiaobai__quanyi"] = "权宜 已换位",
}

quanyi_viewas:addEffect("viewas", {
  pattern = ".",
  interaction = function (self, player)
    local used_cards = player:getTableMark("@$xiaobai__quanyi")
    if used_cards == 0 then used_cards = {} end
    local all_names = Fk:getAllCardNames("bt")
    return UI.CardNameBox {
      choices = player:getViewAsCardNames("xiaobai__quanyi", all_names, nil, used_cards),
      all_choices = all_names,
      default_choice = "AskForCardsChosen"
    }
  end,
  card_filter = Util.FalseFunc;
  view_as = function (self, player, cards)
    if self.interaction.data == "AskForCardsChosen" or nil then return nil end
    return Fk:cloneCard(self.interaction.data)
  end
})

local quanyi = fk.CreateSkill{
  name = "xiaobai__quanyi",
}
quanyi:addEffect(fk.TurnEnd, {
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player:getLastAlive() == target and player:usedSkillTimes(quanyi.name, Player.HistoryRound) < 1
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local next = player:getNextAlive()
    return room:askToSkillInvoke(player,{ 
      skill_name = quanyi.name, 
      prompt = "#xiaobai__quanyi_prompt::"..next.id})
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local next = player:getNextAlive() ---@type ServerPlayer
    local did =  next:getMark("@@xiaobai__quanyi") > 0
    room:swapSeat(player, next)
    room:addPlayerMark(next, "@@xiaobai__quanyi")
    if player.dead then return end
    local _,use = room:askToUseActiveSkill(player,{
      skill_name = quanyi_viewas.name,
      prompt = "#xiaobai__quanyi_use_prompt"})
    if not use then return end
    
    local card = Fk:cloneCard(use.interaction)
    card.skillName = quanyi.name
    room:useCard{
      card = card,
      from = player,
      tos = table.map(use.targets, function(e) return {e} end),
      skillName = quanyi.name
    }
    if player.dead then return end
    room:addTableMark(player, "@$xiaobai__quanyi", use.interaction)
    if did then
      local cardNames = player:getTableMark("@$xiaobai__quanyi")
      if cardNames == 0 then return end
      for _, cardName in ipairs(cardNames) do
        room:askToUseVirtualCard(player,{
          name = cardName,
          skill_name = quanyi.name})
      end
      room:handleAddLoseSkills(player, "-"..quanyi.name)
    end
  end,
})

quanyi:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$xiaobai__quanyi", 0)
end)

local skels = {}
table.insert(skels, quanyi)
table.insert(skels, quanyi_viewas)

return skels